﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BioAttendance
{
    public partial class VerificationForm : Form
    {
        public VerificationForm(BioAttendanceEntities dbEntities)
        {
            InitializeComponent();
            foreach(Employee emp in dbEntities.Employees)
            {
                this.fingerPrintVerificationUserControl.Samples.Add(new DPFP.Template(new MemoryStream(emp.FingerPrint)),emp);
            }
        }

        delegate void Function();

        private bool verified = false;
        public bool Verified
        {
            get
            {
                return this.verified;
            }
            private set
            {
                this.verified = value;
                this.buttonRecord.Enabled = value;
            }
        }

        private Employee verifiedEmployee;
        public Employee VerifiedEmployee 
        {
            get
            {
                return this.verifiedEmployee;
            }

            private set
            {
                this.verifiedEmployee = value;
                if (value != null)
                {
                    this.EmpPictureBox.Image = new Bitmap(new MemoryStream(value.Photo));
                    SetName(value.Name);
                    AttendanceTime = DateTime.Now;
                    this.Verified = true;
                }
                else
                {
                    this.EmpPictureBox.Image = null;
                    SetName(string.Empty);
                    AttendanceTime = null;
                    this.Verified = false;
                }
            }
        }

        private DateTime? attendanceTime;
        public DateTime? AttendanceTime
        {
            get
            {
                return attendanceTime;
            }

            private set
            {
                this.attendanceTime = value;
                SetTime(this.attendanceTime);
            }
        }

        public String Note
        {
            get
            {
                String note = this.textBoxNote.Text;
                if (note.Length > 255)
                {
                    return note.Substring(0, 255);
                }
                {
                    return note;
                }
            }
        }

        private void fingerPrintVerificationUserControl1_VerificationStatusChanged(object sender, StatusChangedEventArgs e)
        {
            if (e.Status)
            {
                this.VerifiedEmployee = (Employee)this.fingerPrintVerificationUserControl.VerifiedObject;
            }
            else
            {
                this.VerifiedEmployee = null;
            }
        }

        private void SetName(string prompt)
        {
            this.Invoke(new Function(delegate()
            {
                labelName.Text = "Name: "+ prompt;
            }));
        }

        private void SetTime(DateTime? dt)
        {
            this.Invoke(new Function(delegate()
            {
                if (dt != null)
                {
                    labelTime.Text = "Time: " + dt.Value.ToShortDateString() + " " + dt.Value.ToShortTimeString();
                }
                else
                {
                    labelTime.Text = "Time: (Not recorded)";
                }
            }));
        }

        private void buttonRecord_Click(object sender, EventArgs e)
        {
            if (this.Verified)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
                this.Close();
            }
        }
    }
}
